package com.colorlight.cloud.xm.entity.instructions;

import com.colorlight.cloud.xm.api.XmEntityScheduleActions;
import com.colorlight.cloud.xm.entity.po.SenderAuthorizationInfo;
import com.colorlight.iot.proto.xm.instruction.XmInstruction;
import com.google.protobuf.ByteString;
import com.google.protobuf.InvalidProtocolBufferException;
import lombok.*;
import lombok.extern.slf4j.Slf4j;

import java.io.Serializable;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author Demon
 * @description 获取设备信息
 */

@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Slf4j
public class GetDeviceAuthInfo implements XmEntityScheduleActions {
	private InputMsg input;
	private OutputMsg output;
	
	@Data
	@NoArgsConstructor
	@Builder
	public static class InputMsg implements Serializable {
		/**
		 *
		 * 无参数，点检所有模组
		 */
	}
	
	@Data
	@NoArgsConstructor
	@AllArgsConstructor
	@Builder
	public static class OutputMsg implements Serializable {
		private List<SenderAuthorizationInfo> senderAuthorizationInfo;
	}
	
	
	
	@Override
	public ByteString objectConvertBytes() {
		XmInstruction.GetDeviceAuthInfo.Builder builder = XmInstruction.GetDeviceAuthInfo.newBuilder();
		XmInstruction.GetDeviceAuthInfo.InputMsg.Builder inputMsg = XmInstruction.GetDeviceAuthInfo.InputMsg.newBuilder();
		builder.setInput(inputMsg);
		return builder.build().toByteString();
	}
	
	@Override
	public GetDeviceAuthInfo byteConvertObject(ByteString bytes) {
		XmInstruction.GetDeviceAuthInfo getDeviceAuthInfo = null;
		try {
			getDeviceAuthInfo = XmInstruction.GetDeviceAuthInfo.parseFrom(bytes);
		} catch (InvalidProtocolBufferException e) {
			log.error("", e);
		}
		Objects.requireNonNull(getDeviceAuthInfo);
		GetDeviceAuthInfo.OutputMsg outputMsg = new GetDeviceAuthInfo.OutputMsg();
		List<SenderAuthorizationInfo> collect = getDeviceAuthInfo.getOutput().getSenderAuthorizationInfoList().stream().map(e -> new SenderAuthorizationInfo().formProto(e)).collect(Collectors.toList());
		outputMsg.setSenderAuthorizationInfo(collect);
		this.setOutput(outputMsg);
		return this;
	}
}
